define(['plugins/echarts/echarts.min'], function (echarts) {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_where = {},
        cols = [],
        moduleName = '拜访统计';
        editArea = ['800px', '400px']
    ;

    app.newModule({
        init: function () {
            self = this;
            self.data = {
                list: [],
                names: [],
                startDate: '',
                endDate: '',
                div1: true,
                div2: false,
                hours: [],
                todayHours: [],
                counts: [],
                todayCounts:[]
            };
            cols=[[
                {field: 'ROW_ID',title: '序号',width: 60, fixed: 'left'}
                ,{field: 'NAME',title: '人员',width: 150, fixed: 'left',sort:true}
                ,{field: 'todayHours',title: '当天拜访时数',width: 80,sort:true,search:'default'}
                ,{field: 'todayCounts',title: '当天拜访家数',width: 80,sort:true,search:'default'}
                ,{field: 'hours',title: '本期拜访时数',width: 80,sort:true}
                , {field: 'counts',title: '本期拜访家数',width: 80,sort:true}
            ]]
            let _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'bftj',
                _loadTplsFull: [{
                    dom: 'bftj-info', tpl: 'bftj-info'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: '#bftj',
                    title: moduleName + '信息表',
                    id: 'bftj',
                    toolbar: '#table-toolbar',
                    defaultToolbar: [],
                    height: 'full',
                    cols: cols,
                    data:self.data.list,
                    loading: true,
                    page: true,
                    where: table_where,
                    done: function (res, curr, count) {

                    }
                });


                //监听行双击事件
                table.on('rowDouble(bftj_table)', function (obj) {
                    self._openTplFull({
                        tag: 'bftj-info',
                        tagData: {
                            item: obj,
                            startDate:self.data.startDate,
                            endDate:self.data.endDate,
                        },
                        index: 0,
                        openOpts: {
                            title: '查看',
                            area:editArea,
                            maxmin: true
                        }
                    });
                });
            });

        },
        //初始化数据
        initData: function () {

            app.ajax({
                xurl: "/bb/bftj/getList",
                xdata: {
                    startDate: self.data.startDate,
                    endDate: self.data.endDate
                },
                loading:true,
                cb: function (res) {
                    if (res.ok) {
                        var data = res.data.data;
                        self.data.list = data;
                        self.data.list.forEach(function (d, index) {
                            d.ROW_ID=index+1;
                            self.data.counts.push(d.counts);
                            self.data.todayCounts.push(d.todayCounts);
                            self.data.hours.push(d.hours)
                            self.data.todayHours.push(d.todayHours);
                            self.data.names.push(d.NAME)
                        })
                        self.formChat();
                        self.initTable();
                    }
                }
            });
        },
        formChat: function () {
            var chartDiv = echarts.init(doc.getElementById('divChart'));

            var option = {
                legend: {
                    data: self.data.names
                },
                grid: {
                    x: 35,
                    x2: 10,
                    y: 30,
                    y2: 25
                },
                // 提示框组件
                tooltip: {
                    trigger: 'axis', // 触发类型。坐标轴触发，主要在柱状图，折线图等会使用类目轴的图表中使用/none什么都不触发
                },
                toolbox: {
                    show: false,
                    feature: {
                        mark: {
                            show: true
                        },
                        dataView: {
                            show: true,
                            readOnly: false
                        },
                        magicType: {
                            show: true,
                            type: ['bar']
                        },
                        restore: {
                            show: true
                        },
                        saveAsImage: {
                            show: true
                        }
                    }
                },
                calculable: false,
                xAxis: [{
                    type: 'category',
                    data: self.data.names
                }],
                yAxis: [{
                    type: 'value',
                    axisLabel: {
                        formatter: function (d) {
                            return d;
                        }
                    },
                    splitArea: {
                        show: true
                    }
                }],
                series: [{
                    name: '当天拜访',
                    type: 'bar',
                    data: self.data.div1 == true ? self.data.todayHours : self.data.todayCounts,
                    label: {
                        normal: {
                            formatter: function (a) {
                                return a.data;
                            },
                            show: true,
                        }
                    },
                },
                    {
                        name: '本期拜访',
                        type: 'bar',
                        data: self.data.div1 == true ? self.data.hours : self.data.counts,
                        label: {
                            normal: {
                                formatter: function (a) {
                                    return a.data;
                                },
                                show: true,
                            }
                        },
                    },]
            };
            chartDiv.setOption(option);
            self.data.counts.pushArray([],true);
            self.data.todayCounts.pushArray([],true);
            self.data.hours.pushArray([],true);
            self.data.todayHours.pushArray([],true);
            self.data.names.pushArray([],true);
        },
        reloadTableCurr: function () {
            self._reloadTable({id: 'bftj', where: table_where});
        },
        _ready: function () {
            this._super({
            });

            var month = new Date().getCurrentMonth();
            self.data.startDate = month[0];//本月开始日期
            self.data.endDate = new Date().Format('yyyy-MM-dd');

            new Vue({
                el:document.getElementById('page'),
                data:{
                    dateRange:[self.data.startDate,self.data.endDate],
                    d:self.data
                },
                watch:{
                  'dateRange':function (res) {
                    var startDate=  new Date(res[0]);
                    var startmonth=startDate.getMonth()+1;
                    var startday=startDate.getDate();
                    var endDate=  new Date(res[1]);
                    var endmonth=endDate.getMonth()+1;
                    var endday=endDate.getDate();
                    self.data.startDate =startDate.getFullYear()+'-'+(startmonth<10?'0'+startmonth:startmonth)+'-'+(startday<10?'0'+startday:startday);
                    self.data.endDate = endDate.getFullYear()+'-'+(endmonth<10?'0'+endmonth:endmonth)+'-'+(endday<10?'0'+endday:endday);
                    console.log(self.data);
                    self.initData();
                  },
                'd.div1':function (res) {
                        self.data.div1 = res;
                        self.formChat()
                  },
                },
                methods:{
                }

            })
            self.initData();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

});